﻿Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.IO
Imports System.Text
Imports System.Runtime.Serialization.Json

Public Class JsonHelper
    Public Shared Function ToJsonString(ByVal o As Object) As String
        If Not o.[GetType]().IsSerializable Then
            Throw New ArgumentException("Object has to be serialized.")
        End If

        Dim serializer As New DataContractJsonSerializer(o.[GetType]())
        Dim ms As New MemoryStream()
        serializer.WriteObject(ms, o)
        Return Encoding.[Default].GetString(ms.ToArray())
    End Function

End Class
